<?php

namespace app\manage\controller;

use app\Request;
use hg\apidoc\annotation as Apidoc;
use think\response\Json;
use app\manage\services\RoleAuth as RoleService;
use app\manage\validate\Role as RoleValidate;
use think\App;

/**
 * @Apidoc\Title("角色模块")
 * @Apidoc\Group("")
 */
class RoleAuth extends Base
{
    protected $service;
    protected $validate;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->service = new RoleService();
        $this->validate = new RoleValidate();
    }

    /**
     * @Apidoc\Title("查询分页列表")
     * @Apidoc\Url("/manage/role/list")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param(ref="pagingParam")
     * @Apidoc\Returned(ref="pagingParam")
     * @Apidoc\Param("name",type="varchar",desc="角色名称")
     * @Apidoc\Returned("data",type="array",ref="app\model\Role\getList",withoutField="")
     */
    public function getPageList(Request $request): Json
    {
        $page = $request->param("pageIndex", config('app.defaultPage'));
        $limit = $request->param("pageSize", config('app.defaultLimit'));
        $where = [];
        $name = $request->param("name", '');
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        $res = $this->service->getPageList($page, $limit, $where);
        return jsonT($res);
    }

    /**
     * @Apidoc\Title("权限列表")
     * @Apidoc\Url("/manage/role/authList")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Returned("data",type="array",ref="app\model\Auth\getList",withoutField="")
     */
    public function getAuthList(): Json
    {
        $res = $this->service->getAuthList();
        return jsonT($res);
    }

    /**
     * @Apidoc\Title("根据ID查询明细")
     * @Apidoc\Url("/manage/role/detail")
     * @Apidoc\Param("id",type="int",require=true,desc="唯一id")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Returned(ref="app\model\Role\getInfoById",withoutField="")
     */
    public function detail(Request $request): Json
    {
        if (!empty($this->validate)) {
            $this->validate->goCheck('info');
        }
        $params = $request->param();
        $res = $this->service->getInfoById($params['id']);
        return jsonT($res);
    }

    /**
     * @Apidoc\Title("新增")
     * @Apidoc\Url("/manage/role/add")
     * @Apidoc\Method("POST")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param (ref="app\model\Role\getInfoById",withoutField="id,create_at,update_at,")
     * @Apidoc\Returned (ref="app\model\Role\getInfoById")
     */
    public function add(Request $request): Json
    {
        $params = $request->post();
        $params['auth_ids'] = json_encode($params['auth_ids']);
        $res = $this->service->add($params);
        return jsonT($res);
    }

    /**
     * @Apidoc\Title("编辑")
     * @Apidoc\Url("/manage/role/edit")
     * @Apidoc\Method("PUT")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param (ref="app\model\Role\getInfoById",withoutField="create_at,update_at,")
     * @Apidoc\Returned (ref="app\model\Role\getInfoById")
     */
    public function edit(Request $request): Json
    {
        if (!empty($this->validate)) {
            $this->validate->goCheck('edit');
        }
        $params = $request->post();
        $res = $this->service->update($params);
        return jsonT($res);
    }

    /**
     * @Apidoc\Title("删除")
     * @Apidoc\Url("/manage/role/delete")
     * @Apidoc\Method("DELETE")
     * @Apidoc\Param("id",type="int",require=true,desc="唯一id")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Returned("data",type="boolean",desc="删除状态")
     */
    public function delete(Request $request): Json
    {
        if (!empty($this->validate)) {
            $this->validate->goCheck('delete');
        }
        $params = $request->post();
        $res = $this->service->delete($params['id']);
        return jsonT($res);
    }

    /**
     * @Apidoc\Title("设置账号角色")
     * @Apidoc\Url("/manage/role/setRole")
     * @Apidoc\Method("PUT")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param("id",type="int",require=true,desc="账号ID")
     * @Apidoc\Param("rid",type="int",require=true,desc="职位角色ID")
     * @Apidoc\Returned("data",type="boolean",desc="设置状态")
     */
    public function setRole(Request $request): Json
    {
        if (!empty($this->validate)) {
            $this->validate->goCheck('setRole');
        }
        $params = $request->post();
        $res = $this->service->setting($params);
        return jsonT($res);
    }
}
